package com.neutech.maplestory.entity;
import com.neutech.maplestory.client.MapleStoryClient;
import com.neutech.maplestory.constant.Constant;
import com.neutech.maplestory.util.ImageUtil;
import java.awt.*;
import java.util.List;
import java.util.Random;

/**
 * 弓箭类的创建
 */

public class Arrow extends AbstractMapleStoryObject{
    public  static Image[] imgs ={
            ImageUtil.getImage("hero_right_arrow"),
            ImageUtil.getImage("hero_left_arrow")
    };
    public Arrow(){
        this.speed = 30;
        this.width = imgs[0].getWidth(null);
        this.height = imgs[1].getHeight(null);
        this.live = true;
    }
    public Arrow(MapleStoryClient msc ,int x, int y, Direction dir ){
        this();
        this.msc = msc;
        this.x = x;
        this.y = y;
        this.dir = dir;
    }
    @Override
    public void draw(Graphics g) {
        if(!live){
            msc.arrows.remove(this);
            return;
        }
        switch (dir){
            case RIGHT:
                g.drawImage(imgs[0],x,y,null);
                break;
            case LEFT:
                g.drawImage(imgs[1],x,y,null);
                break;
            default:
                break;
        }
        move();

    }

    @Override
    public void move() {
        switch (dir){
            case RIGHT:
                this.x += speed;
                break;
            case LEFT:
                this.x -= speed;
                break;
            default:
                break;
        }
        outOfBound();
    }
    private void outOfBound(){
        if (this.x<-500||this.x> Constant.GAME_WIDTH+500){
            this.live=false;
        }
    }
    /**
     * 弓箭打怪的方法
     * @param mob  被击中的怪物
     * @return  是否击中
     */
    public static Random random = new Random();
    private boolean hit(Mob mob){
        if(this.live && this.getRectangle().intersects(mob.getRectangle())&&mob.action !=Action.DIE){
            this.live = false;
       //   msc.hero.hitValue = random.nextInt(msc.hero.ATT  -5) +8;       //8~12
            msc.hero.hitValue =(int)(msc.hero.ATT*(1+ random.nextDouble()));
            //画出伤害值
            Power power =new Power(msc,mob.x,mob.y);
            msc.powers.add(power);
            mob.HP -= power.value;
            if( mob.HP<=0){
                mob.action = Action.DIE;
                msc.hero.EXP += 100;
                if (msc.hero.EXP == msc.hero.MAX_EXP){
                    msc.hero.EXP = 0;
                    msc.hero.MAX_MP += 100;
                    msc.hero.MAX_HP += 100;
                    msc.hero.ATT += msc.hero.ATT ;
                }
                if(random.nextInt(100)<70){
                    Item item = new Item(msc,mob.x + mob.width / 2 -14,mob.y + mob.height - 30,random.nextInt(4));
                    msc.items.add(item);
                }

            }

  //          mob.live = false;

            return true;
        }
        return  false;
    }

    /**
     * 一只弓箭与所有的怪物进行碰撞检测的方法
     * @param mobs 怪物容器

     * @return 是否
     */
    public boolean hit (List<Mob> mobs){
        for (int i = 0; i < mobs.size(); i++) {
             Mob mob = mobs.get(i);
             if(hit(mob)){
                 return  true;
             }
        }
        return false;
    }
}
